// 1、将数字转为星期，用map
let arr = [0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 6]
let arr2 = arr.map((item) => {
    const numberToChinese = {0: '周日', 1: '周一', 2: '周二', 3: '周三', 4: '周四', 5: '周五', 6: '周六'}
    return numberToChinese[item]
})
console.log(arr2) // ['周日', '周一', '周二', '周二', '周三', '周三', '周三', '周四', '周四', '周四', '周四','周六']
// 2、找出大于60的成绩
let scores = [95, 91, 59, 55, 42, 82, 72, 85, 67, 66, 55, 91]
let scores2 = scores.filter((item) => item > 60)
console.log(scores2) //  [95,91,82,72,85,67,66, 91]
// 3、求所有奇数和
let sum = scores.reduce((sum, n) => {
    // if (n%2!=0){sum+=n} return sum
    return n % 2 === 0 ? sum : sum + n
}, 0)
console.log(sum) // 奇数之和：598
/* 4、数据变换
let animalArr = [
{名称:'动物',id:1,parent:null},
{名称:'狗狗',id:2,parent:1},
{名称:'猫咪',id:3,parent:1},
]
转化为
{
    id:1, '名称':'动物',children:[
{名称:'狗狗',id:2,children:null},
{名称:'猫咪',id:3,children:null},
    ]
}
*/
let animalArr = [
    {名称: '动物', id: 1, parent: null},
    {名称: '狗狗', id: 2, parent: 1},
    {名称: '猫咪', id: 3, parent: 1},
]
animalArr.reduce((result, item) => {
    if (item.parent === null) {
        result.id = item.id
        result['名称'] = item['名称']
    } else {
        result.children.push(item)
        delete item.parent
        item.children = null
    }
    return result;
}, {id: null, children: []})
// 5、寻找数组中的5的倍数 find只要有返回true就停止
let arr3 = [1, 2, 5, 9, 8, 7, 3]
let near = arr3.find(function (x) {
    return x % 5 === 0
})
let nearIndex = arr3.findIndex(function (x) {
    return x % 5 === 0
})
console.log(near + "，" + nearIndex)
// 6、反转字符串
console.log("123".split('').reverse().join(''))
// 7、数组排序
console.log(arr3.sort(
    function (a, b) {
        if (a > b) {
            return -1
        } else if (a = b) {
            return 0
        } else {
            return 1
        }
    }
))
